<template>
  <div class="container">
    <h3 class="title"><span />所有文件</h3>
    <el-button size="small" type="primary" icon="el-icon-arrow-left" class="back" @click="prev">返回</el-button>
    <div class="content">
      <div class="left">
        <el-tree
          :data="treeData"
          :props="defaultProps"
          :expand-on-click-node="false"
          :check-on-click-node="true"
          highlight-current
          @node-click="handleNodeClick"
        />
      </div>

      <div class="right">
        <el-table :data="filesData">
          <el-table-column label="文件名" prop="title">
            <template slot-scope="scope">
              <img :src="require(`../icon/${scope.row.type}.png`)" alt="" class="fileIcon">
              <span>{{ scope.row.title }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right">
            <template slot-scope="scope">
              <el-button v-if="scope.row.type !='path'" class="edit" size="mini" @click="getInfo(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
        <BouncedDialog id="file" title="编辑文件" :dialog-show.sync="dialogShow" :width="80">
          <div class="btn">
            <!-- <el-button @click="dialogShow = false">取 消</el-button> -->
            <el-button type="primary" class="save" size="mini" @click="submit()">保存</el-button>
          </div>
          <Editor
            ref="editor"
            v-model="tempForm.content"
            height="500"
            width="100%"
            :options="{
              enableBasicAutocompletion: true,
              enableSnippets: true,
              enableLiveAutocompletion: true,
              tabSize:6,
              fontSize:14,
              showPrintMargin:false, //去除编辑器里的竖线
              tabSize:2,//换行符大小
            }"

            :lang="'html'"
            theme="monokai"
            @init="editorInit"
          />

        </BouncedDialog>
      </div>

    </div>
  </div>
</template>

<script>
import mouldApi from '@/api/theme'
import mould from '@/api/theme'
import Editor from 'vue2-ace-editor'
export default {
  components: {
    Editor
  },
  data() {
    return {
      website_id: '',
      treeData: [],
      defaultProps: {
        children: 'child',
        label: 'title'
      },
      path: '',
      filesData: [],
      dialogShow: false,
      tempForm: {
        content: '',
        path: ''
      },
      lang: '',
      tempRules: {
        content: [
          { required: 'true', message: '内容不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.website_id = this.$route.query.id
    this.lang = this.$route.query.lang
    this.getFilesPath()
  },
  methods: {
    editorInit() {
      require('brace/theme/monokai')
      // for (let i = 0;i < this.arr.length;i++) {
      //   require('brace/theme/' + this.arr[i])
      // }
      require('brace/mode/html')
      require('brace/mode/json')
      require('brace/snippets/json')
      require('brace/snippets/html')
      require('brace/snippets/css')
    },
    handleNodeClick(val) {
      this.path = val.path
      this.getFiles()
    },
    // 获取文件目录
    getFilesPath() {
      mouldApi.allFilePath({ website_id: this.website_id, lang: this.lang }).then(res => {
        this.treeData = res.data
        if (res.data.length > 0) {
          this.path = res.data[0].path
          this.getFiles()
        }
      })
    },
    // 获取文件
    getFiles() {
      const val = {
        website_id: this.website_id,
        path: this.path,
        lang: this.lang
      }
      mouldApi.allFiles(val).then(res => {
        this.filesData = res.data
      })
    },
    // 源码查看
    getInfo(row) {
      this.tempForm.path = this.website_id + row.path
      mouldApi.fileRead({ path: this.website_id + row.path }).then(res => {
        this.tempForm.content = res.data
        this.dialogShow = true
      })
    },
    // 提交编辑
    submit() {
      mouldApi.fileUpdate({ ...this.tempForm }).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
          this.dialogShow = false
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    prev() {
      this.$router.push({ name: 'Theme' })
    }
  }
}
</script>
<style lang='scss'>
#file{
  .el-dialog{
    background-color: #282722;
  }
  .el-dialog__header{
    padding: 10px 20px 1px;
    .el-dialog__title{
      color: #fff;
      font-size: 16px;
      // font-weight: bold;
    }
  }
  .el-dialog__body{
    padding: 0;
  }
  .save.el-button--primary{
    width: 54px;
    height: 25px;
    border-color: #565656;
    background-color: #565656;
    font-size: 12px;
  }
  .btn{
    margin-top: 0;
    margin-bottom: 10px;
    margin-left: 15px;
    text-align: left;
  }
}
</style>
<style scoped lang='scss'>
.back{
    top: 16px;
    right: 21px;
    }
.content{
  margin-top: 36px;
    display: flex;
    justify-content: left;
    .left{
        width: 220px;
        margin-right: 30px;
        background-color: #fff;
        padding: 20px;
    }
    .right{
        width: 100%;
        background-color: #fff;
        padding: 20px;
        .fileIcon{
          width: 17px;
          vertical-align: middle;
        }
    }
}
</style>
